package com.intelligence.tio.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;

import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioAbsAioHandler;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.JoinGroupReqBody;
import com.intelligence.tio.common.packages.JoinGroupRespBody;

/**
* @Title: JoinGroupReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 加入组消息请求处理器 
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:31:22 
* @version V1.0
 */
public class JoinGroupReqHandler extends AbsTioBsHandler<JoinGroupReqBody>
{
	private static Logger log = LoggerFactory.getLogger(JoinGroupReqHandler.class);

	/** 
	 * @return
	 * @author: tanyaowu
	 */
	@Override
	public Class<JoinGroupReqBody> bodyClass()
	{
		return JoinGroupReqBody.class;
	}

	/** 
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public Object handler(JsonPacket packet, JoinGroupReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到进群请求消息:{}", Json.toJson(bsBody));
		JoinGroupRespBody joinGroupRespBody = new JoinGroupRespBody();
		
		joinGroupRespBody.setGroup(bsBody.getGroup());
		
		try {
			Aio.bindGroup(channelContext, bsBody.getGroup());
			joinGroupRespBody.setStatus(1);
			joinGroupRespBody.setMsg("成功加入【 "+bsBody.getGroup()+" 】");
		} catch (Exception e) {
			e.printStackTrace();
			joinGroupRespBody.setStatus(0);
			joinGroupRespBody.setMsg("加入【 "+bsBody.getGroup()+" 】失败！");
		}
		
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(joinGroupRespBody).getBytes(JsonPacket.CHARSET));
		Aio.send(channelContext, respPacket);
		return null;
	}
}
